/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

package com.example.service.impl;

import com.example.constant.ChangeStatus;
import com.example.constant.IsvProduceConstant;
import com.example.model.IMessageResp;
import com.example.model.IsvProduceReq;
import com.example.model.RefreshReq;
import com.example.model.ReleaseReq;
import com.example.model.UpdateStatusReq;
import com.example.service.LicenseProduceService;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

import com.example.constant.Activity;
import com.example.constant.RenewAction;
import com.example.util.ResultCodeEnum;

@Component
public class LicenseProduceServiceImpl implements LicenseProduceService {

    /**
     * 使用Collections.unmodifiableMap修饰可变集合，保证在方法处理过程中不会对常量方法成员误删除、修改等操作
     */
    private final Map<String, Function<IsvProduceReq, IMessageResp>> isvProduceConsumers
        = Collections.unmodifiableMap(initConsumers());


    /**
     * 根据活动类型调用对应的实现方法
     */
    private Map<String, Function<IsvProduceReq, IMessageResp>> initConsumers() {
        Map<String, Function<IsvProduceReq, IMessageResp>> consumers = new HashMap<>();
        consumers.put(Activity.REFRESH, this::refresh);
        consumers.put(Activity.UPDATE, this::updateStatus);
        consumers.put(Activity.RELEASE, this::release);
        return consumers;
    }


    @Override
    public IMessageResp processProduceReq(IsvProduceReq isvProduceReq) {
        return isvProduceConsumers.get(isvProduceReq.getActivity()).apply(isvProduceReq);
    }

    /**
     * 变更授权码
     * @param isvProduceReq 入参
     * @return 响应结果
     */
    private IMessageResp refresh(IsvProduceReq isvProduceReq) {
        IMessageResp resp = new IMessageResp();
        resp.setResultCode(ResultCodeEnum.SUCCESS.getResultCode());
        resp.setResultMsg(ResultCodeEnum.SUCCESS.getResultMsg());

        // 入参转为续费对象 可自添加必填项校验
        RefreshReq req = new RefreshReq();
        BeanUtils.copyProperties(isvProduceReq, req);

        // 调测数据不做业务处理
        if (IsvProduceConstant.DEBUG_TEST.equals(req.getTestFlag())) {
            return resp;
        }

        // 根据license到数据库查到详情信息，然后根据操作类型做对应的业务处理

        switch (req.getScene()) {
            case RenewAction.RENEW:
                // RENEWAL：续费

                break;
            case RenewAction.UNSUB:
                // UNSUBSCRIBE_RENEWAL_PERIOD：退续费

                break;
            default:
                break;
        }
        return resp;
    }

    /**
     * 变更授权码状态
     * @param isvProduceReq 请求入参
     * @return 过期处理结果
     */
    private IMessageResp updateStatus(IsvProduceReq isvProduceReq) {
        IMessageResp resp = new IMessageResp();
        resp.setResultCode(ResultCodeEnum.SUCCESS.getResultCode());
        resp.setResultMsg(ResultCodeEnum.SUCCESS.getResultMsg());

        // 转为续费对象 可自添加必填项校验
        UpdateStatusReq req = new UpdateStatusReq();
        BeanUtils.copyProperties(isvProduceReq, req);

        // 调测数据不做业务处理
        if (IsvProduceConstant.DEBUG_TEST.equals(isvProduceReq.getTestFlag())) {
            return resp;
        }

        // 根据license到数据库查到详情信息，然后根据状态做对应的业务处理

        switch (req.getStatus()) {
            case ChangeStatus.FREEZE:
                // 冻结

                break;
            case ChangeStatus.UNFREEZE:
                // 解冻

                break;
            default:
                break;
        }
        return resp;
    }

    /**
     * 释放授权码
     * @param isvProduceReq 请求入参
     * @return 释放操作结果
     */
    private IMessageResp release(IsvProduceReq isvProduceReq) {
        IMessageResp resp = new IMessageResp();
        resp.setResultCode(ResultCodeEnum.SUCCESS.getResultCode());
        resp.setResultMsg(ResultCodeEnum.SUCCESS.getResultMsg());

        // 转为释放资源对象 可自添加必填项校验
        ReleaseReq req = new ReleaseReq();
        BeanUtils.copyProperties(isvProduceReq, req);

        // 调测数据不做业务处理
        if (IsvProduceConstant.DEBUG_TEST.equals(isvProduceReq.getTestFlag())) {
            return resp;
        }

        // 根据license到数据库查到详情信息，然后实现资源释放逻辑

        return resp;
    }
}
